import { formatCurrency, isNotEmpty } from '@/utils'
import dayjs from 'dayjs'
import Pagination from '@/components/Pagination'
// 自定义表格工具扩展
import RightToolbar from '@/components/RightToolbar'
import serviceUtil from '@/utils/service'
import storeUtil from '@/utils/store'
import { showError, resetForm, showSuccess } from '@/utils'
import onlyNumber from '@/utils/onlyNumber'
import globalMixin from '@/mixin/global'
export default {
  // 构造全局方法
  buildPrototype(Vue) {
    // 当前的 baseUrl
    Vue.prototype.$baseUrl = process.env.BASE_URL
    // 当前版本
    Vue.prototype.$version = process.env.VUE_APP_VERSION
    // 构建时间
    Vue.prototype.$buildTime = process.env.VUE_APP_BUILD_TIME
    Vue.prototype.resetForm = resetForm
    Vue.prototype.showSuccess = showSuccess
    Vue.prototype.showError = showError
    Vue.prototype.serviceUtil = serviceUtil
    Vue.prototype.storeUtil = storeUtil
    Vue.prototype.dayjs = dayjs
  },
  /**
   * @Description: 注册全局mixin
   * @author tangyuhui
   * @date 2021/5/5 12:18 下午
  */
  buildGlobalMixin(Vue) {
    Vue.mixin(globalMixin)
  },
  installPlugin(Vue) {
    // 全局组件挂载
    Vue.component('Pagination', Pagination)
    Vue.component('RightToolbar', RightToolbar)
    // 限制输入数字
    Vue.use(onlyNumber)
  },
  buildFilter(Vue) {
    Vue.filter('currency', function(value) { // 全局方法 Vue.filter() 注册一个自定义过滤器,必须放在Vue实例化前面
      return formatCurrency(value)
    })
    Vue.filter('idcardMask', function(value) {
      if (!isNotEmpty(value)) return '--'
      let regExp = null
      if (value.length === 15) {
        regExp = /(.{9})(.{6})/gi
      } else if (value.length === 18) {
        regExp = /(.{12})(.{6})/gi
      } else {
        return value
      }
      return value.replace(regExp, '$1******')
    })
    Vue.filter('txt', function(value) {
      if (!isNotEmpty(value)) return '--'
      return value
    })
    Vue.filter('txtSlash', function(value) {
      if (!isNotEmpty(value)) return '/'
      return value
    })
    Vue.filter('formatTime', function(value) {
      if (!value) return '/'
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    })
    Vue.filter('formatDate', function(value) {
      if (!value) return '/'
      return dayjs(value).format('YYYY-MM-DD')
    })
  },
  buildDirective(Vue) {
    // 自定义focus指令
    Vue.directive('focus', {
      inserted: function(el) {
        el.focus()
      }
    })
  }
}
